package scuts.core.types;
import utest.Assert;

/**
 * ...
 * @author 
 */

class ProgressiveFutureTest 
{

  public function new() 
  {
    
  }
  
  public function test_states () {
    var f = new ProgressiveFuture();
    Assert.isFalse(f.isDone());
    Assert.isFalse(f.isDelivered());
    Assert.isFalse(f.isCanceled());
    f.deliver(5);
    Assert.isFalse(f.isCanceled());
    Assert.isTrue(f.isDone());
    Assert.isTrue(f.isDelivered());
    
  }
  
  public function test_onComplete () {
    var f = new ProgressiveFuture();
    var res:Int = 0;
    f.deliverTo(function (v) res = v); 
    f.deliver(5);
    Assert.equals(5, res);
    
    
  }
  
  public function test_Progress () {
    var f = new ProgressiveFuture();
    
    var percent:Float = 0;
    
    
    f.onProgress(function (v) percent = v); 
    f.setProgress(0.5);
    
    Assert.equals(0.5, percent);
    
    f.deliver(5);
    Assert.equals(1, percent);
    
    
  }
  
  public function test_onCompleteAfterSetValue () {
    var f = new ProgressiveFuture();
    var res:Int = 0;
    f.deliver(5);
    f.deliverTo(function (v) res = v); 
    
    Assert.equals(5, res);
  }
  
  public function test_onCancelled () {
    var f = new ProgressiveFuture();
    
    var cancelCalled = false;
    f.ifCanceled(function () cancelCalled = true); 
    f.cancel();
    
    Assert.isTrue(cancelCalled);
  }
  
  public function test_onCancelledAfterResult () {
    var f = new ProgressiveFuture();
    
    var cancelCalled = false;
    f.ifCanceled(function () cancelCalled = true); 
    f.deliver(4);
    f.cancel();
    
    Assert.isFalse(cancelCalled);
  }
  
  public function test_setResultTwice_ThrowError () {
    var f = new ProgressiveFuture();
    
    f.deliver(1);
    Assert.raises(function () f.deliver(4));

  }
  
  public function test_cancelTwice_onCancelledShouldOnlyExecutedOnce () {
    var f = new ProgressiveFuture();
    
    var cancelCalled = 0;
    f.ifCanceled(function () cancelCalled++);
    
    f.cancel();
    f.cancel();
    Assert.equals(cancelCalled, 1);
  }
  
  
  
}